package com.youlai.boot.system.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.LpsOrderYlMapper;
import com.youlai.boot.system.service.LpsOrderYlService;
import com.youlai.boot.system.model.entity.LpsOrderYl;
import com.youlai.boot.system.model.form.LpsOrderYlForm;
import com.youlai.boot.system.model.query.LpsOrderYlQuery;
import com.youlai.boot.system.model.vo.LpsOrderYlVO;
import com.youlai.boot.system.converter.LpsOrderYlConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 永乐服务实现类
 *
 * @author lwb
 * @since 2025-07-18 14:54
 */
@Service
@RequiredArgsConstructor
public class LpsOrderYlServiceImpl extends ServiceImpl<LpsOrderYlMapper, LpsOrderYl> implements LpsOrderYlService {

    private final LpsOrderYlConverter lpsOrderYlConverter;

    /**
    * 获取永乐分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<LpsOrderYlVO>} 永乐分页列表
    */
    @Override
    public IPage<LpsOrderYlVO> getLpsOrderYlPage(LpsOrderYlQuery queryParams) {
        Page<LpsOrderYlVO> pageVO = this.baseMapper.getLpsOrderYlPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取永乐表单数据
     *
     * @param id 永乐ID
     * @return
     */
    @Override
    public LpsOrderYlForm getLpsOrderYlFormData(Long id) {
        LpsOrderYl entity = this.getById(id);
        return lpsOrderYlConverter.toForm(entity);
    }
    
    /**
     * 新增永乐
     *
     * @param formData 永乐表单对象
     * @return
     */
    @Override
    public boolean saveLpsOrderYl(LpsOrderYlForm formData) {
        LpsOrderYl entity = lpsOrderYlConverter.toEntity(formData);
        return this.save(entity);
    }
    
    /**
     * 更新永乐
     *
     * @param id   永乐ID
     * @param formData 永乐表单对象
     * @return
     */
    @Override
    public boolean updateLpsOrderYl(Long id,LpsOrderYlForm formData) {
        LpsOrderYl entity = lpsOrderYlConverter.toEntity(formData);
        return this.updateById(entity);
    }
    
    /**
     * 删除永乐
     *
     * @param ids 永乐ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteLpsOrderYls(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的永乐数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
